<template>
  <Dialog title="退款详情" :loading="loading" :visible="visible" button-text="提交" :reveal="false" class="two-columns" @close="handleClose" @confirm="balanceSubmit">
    <!-- 已退款详情 -->
    <el-row flex style="padding-bottom: 30px">
      <el-col :span="12">{{ currentRow.customerName }}（{{ currentRow.customerPhone | phoneHandle }}）</el-col>
      <el-col :span="12">会员号  {{ currentRow.customerCode }}</el-col>
      <el-col :span="12" class="margin-top">业务单类型  {{ currentRow.businessType }}</el-col>
      <el-col :span="12" class="margin-top">退款单号  {{ currentRow.refundCode }}</el-col>
      <el-col :span="12" class="margin-top">退单人  {{ currentRow.refundMan }}</el-col>
      <el-col :span="12" class="margin-top">退单时间  {{ currentRow.chargebackTime }}</el-col>
      <div class="margin-top clear">
        <ul>
          <li class="detailed">
            退款清单：({{ currentRow.customerCode }})
          </li>
          <li v-for="(items,i) of currentRow.refundFormDetailDtos" :key="i">
            <el-row v-if="currentRow.businessType !== '充值单'" style="width:100%">
              <el-row type="flex">
                <el-col :span="20" class="product">
                  <el-button class="bg" type="text">{{ items.type.split("类")[0] }}</el-button><span style="margin-left:10px;line-height:30px">{{ items.refundLogDto.name }}</span>
                </el-col>
              </el-row>
              <el-row v-if="items.type !== '套餐类'" class="grey" type="flex">
                <el-col :span="20">
                  <span>规格：{{ items.refundLogDto.spec }}</span>
                  <span>单位：{{ items.refundLogDto.unit }}</span>
                </el-col>
              </el-row>
              <el-row type="flex" class="price">
                <el-col :span="6" :offset="14">
                  退货数量：{{ items.amount }}
                </el-col>
                <el-col :span="6">
                  退款金额：￥{{ items.repaymentAmount }}
                </el-col>
              </el-row>
            </el-row>
          </li>
          <li v-if="currentRow.businessType === '充值单'">
            <el-row style="width:100%">
              <el-row type="flex">
                <el-col :span="20" class="product">
                  <el-button class="bg" type="text">充值</el-button><span style="margin-left:10px;line-height:30px">余额退款</span>
                </el-col>
              </el-row>
              <el-row type="flex" class="price">
                <el-col :span="6">
                  退款金额：￥{{ currentRow.repaymentAmount }}
                </el-col>
              </el-row>
            </el-row>
          </li>
        </ul>
      </div>
      <el-col class="margin-top">应退金额<span class="pull-right">¥{{ currentRow.repaymentAmount }}</span></el-col>
      <el-col class="margin-top">退款</el-col>
      <el-col class="margin-top">退款方式<span class="pull-right">{{ currentRow.refundType }}</span></el-col>
      <el-table v-show="currentRow.refundType === '原路返回'" :data="currentRow.logList" border>
        <el-table-column prop="payType" label="退回渠道" />
        <el-table-column prop="payAmount" label="已收金额" />
      </el-table>
      <el-table v-show="currentRow.refundType === '指定账户'" :data="currentRow.logList" border>
        <el-table-column prop="payType" label="退回渠道" />
        <el-table-column prop="account" label="账户" />
        <el-table-column label="退款金额(元)" prop="payAmount" />
      </el-table>
      <el-col class="margin-top"><span class="pull-right">实退合计：<span class="font-color">￥{{ paidIn }}</span></span></el-col>
      <el-col class="margin-top">备注  {{ currentRow.remark }}</el-col>
      <el-col :span="12" class="margin-top">经办人  {{ currentRow.drawer }}</el-col>
      <el-col :span="12" class="margin-top">退款时间  {{ currentRow.refundTime }}</el-col>
    </el-row>
  </Dialog>
</template>
<script>
import Dialog from "@/components/Dialog";
export default {
  filters: {
    phoneHandle(val) {
      if (!val) return '';
      let phone = "";
      phone = val.substring(0, 3) + "****" + val.substring(val.length - 4, val.length);
      return phone;
    },
  },
  components: {
    Dialog,
  },
  props: {
    currentRows: {
      type: Object,
      default: () => ({}),
    },
    visible: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      loading: false,
      datas: {},
      tableData: [{ id: 0, mode: "", account: '' }],
      receivables: 0, // 应收款
      paidIn: 0, // 实收款
      paymentList: [{ id: 0, value: "支付宝" }, { id: 1, value: "微信" }, { id: 2, value: "现金" }, { id: 3, value: "银联" }, { id: 4, value: "POS" }, { id: 5, value: "其他" }],
      balance: 0,
      isBalance: false,
      remark: "",
      refundMethod: '指定账户',
      currentRow: {},
    };
  },
  watch: {
    currentRows: {
      handler(val) {
        this.loading = false;
        this.tableData = [{ id: 0, mode: "", account: '' }];
        this.refundMethod = "指定账户";
        this.paidIn = val.repaymentAmount;
        this.currentRow = JSON.parse(JSON.stringify(val));
        console.log(this.currentRow, 'this.currentRow');
      },
      deep: true,
    },
  },
  methods: {
    handleClose() {
      this.$emit('close');
    },
    addMode() {
      const json = { id: this.tableData.length, mode: "", money: 0 };
      this.tableData.push(json);
    },
    deleteMode(row) {
      this.tableData.splice(row.id, 1);
    },
    balanceChange() {
      this.balance = parseInt(this.balance);
      if (!this.balance) {
        this.balance = 0;
      }
      if (this.balance < 0) {
        this.balance = 0;
      }
      if (this.balance > parseInt(this.currentRow.orderAmount)) {
        this.balance = parseInt(this.currentRow.orderAmount);
      }
      if (this.balance > parseInt(this.currentRow.capital)) {
        this.balance = parseInt(this.currentRow.capital);
      }
    },
    balanceDetermine() {
      this.isBalance = false;
      this.receivables = this.currentRow.orderAmount - this.balance;
      this.tableData[0].money = this.receivables;
      this.paidIn = this.receivables;
      if (this.receivables === 0) {
        this.tableData = [];
      }
    },
    async balanceSubmit() {
      this.loading = true;
      if (parseInt(this.paidIn) !== parseInt(this.currentRow.repaymentAmount)) {
        this.$message.error("实退金额应等于应退金额，请重新输入!");
        this.loading = false;
        return false;
      }
      const list = [];
      if (this.refundMethod === "指定账户") {
        if (!this.tableData[0].mode) {
          this.$message.error("请选择退回渠道!");
          return false;
        }
        if (!this.tableData[0].account) {
          this.$message.error("请输入退回账户!");
          return false;
        }
        const json = {
          account: this.tableData[0].account,
          payType: this.tableData[0].mode,
        };
        list.push(json);
      } else {
        this.currentRow.paymentOrderLogList.forEach(item => {
          const json = {
            payType: item.payType,
            payAmount: item.money,
          };
          list.push(json);
        });
      }
      const res = await this.$api['financialCenter/refund/refundMoney']({
        id: this.currentRow.id,
        actualAmount: this.paidIn,
        // refundMoney: this.currentRow.repaymentAmount,
        remark: this.remark,
        refundType: this.refundMethod,
        logList: list,
      });
      this.loading = false;
      if (res.code !== 200) {
        this.$message.error(res.msg);
        return false;
      }
      this.$message.success("退款成功!");
      this.$emit("success");
    },
    moneyChange() {
      let num = 0;
      this.tableData.forEach(item => {
        num += Number(item.money);
      });
      this.paidIn = num;
    },
    accountChange(val) {
      if (val === "原路返回") {
        this.paidIn = 0;
      } else {
        this.paidIn = this.currentRow.repaymentAmount;
      }
    },
    moneyInput() {
      let money = 0;
      this.currentRow.paymentOrderLogList.forEach(item => {
        const price = item.money ? parseInt(item.money) : 0;
        if (price > parseInt(item.payAmount)) {
          money += parseInt(item.payAmount);
          item.money = item.payAmount;
        } else {
          money += price;
        }
      });
      this.paidIn = money;
      if (this.paidIn > parseInt(this.currentRow.repaymentAmount)) return this.$message.error("实退金额已大于应退金额，请重新输入!");
    },
  },
};
</script>
<style lang="scss" scoped>
.pull-right{
  float: right;
}
.font-color{
  color:#FF6600;
}
.button-color{
  color:#0099FF;
  border-color:#0099FF;
}
.clear{
  clear: both;
}
.detailed{
    margin: 15px 0;
  }
  .el-col-offset-8,.el-col-offset-2 {
  .el-button {
    margin-top: 15px;
    width: 100%;
    height: 40px;
  }
}
/deep/.el-form-item__content{
  margin-left: 50px !important;
}
  .span{
    color: #ff6600;
  }
  .grey{
    color: #aaaaaa;
  }
  .bg{
    background: rgba(211,223,247,0.7);
    width: 50px;
    height: 30px;
    text-align: center;
    border-radius: 10px;
    color: #666;
  }
  .product{
    display: flex;
  }
  .price{
    border-bottom: 1px solid #797979;
    padding-bottom: 10px;
    .el-col{
      display: flex;
    }
  }
  .el-col,.el-col-12{
    line-height: 40px;
  }
  li{
    display: flex;
    margin-top: 5px;
  }
  .detailed{
    margin: 15px 0;
    padding-top: 14px;
  }
  div>span{
    margin-right: 25px;
  }
</style>
